import config from '@/common/config/default.js'
import sign from '@/common/lib/sign.js'
import request from '@/common/lib/requestPlus.js'
import dry from '@/common/lib/dry.js'

var oss = {};

oss.uploadData = function(apiName,data,token){
	var appKey = config.appKey;
	var domain = config.domain;
	var list = config.list;
	var item = list[apiName];
	if(item.domain == ""){
		item.domain = domain;
	}
	var url = item.domain + item.url;
	data = sign.sign(appKey,data,"");
	var header = {
		"content-type": "application/x-www-form-urlencoded",
		"token": token,
		"sign": data.sign
	};
	var getPostData = [];
	getPostData.push({
		url: url,
		method: item.method,
		header: header,
		data: data
	})
	return getPostData;
};

oss.upLoadFileData = function(response, prefix, fileName) {
	var data = response.data.data;
	var fileFormName = "file";
	var host = data.host;
	var formData = {
		"success_action_status": 200,
		"OSSAccessKeyId": data.OSSAccessKeyId,
		"key": data.dir + fileName,
		"policy": data.policy,
		"signature": data.signature,
		"callback": data.callback,
		"oss:domain": host,
		"oss:vw": 0,
		"oss:vh": 0,
		"oss:prefix": prefix,
		"oss:name": prefix
	};
	var upLoadFilesData = [];
	upLoadFilesData.push({
		host: host,
		fileFormName: fileFormName,
		formData: formData,
	})
	return upLoadFilesData;
};

oss.saveImages = function(fileList) {
	return new Promise((resolve) => {
		var fileLists = oss.uploadFile(fileList);
		resolve(fileLists);
	})
}

oss.getUploadData = function(token) {
	var data = {};
	data = request.run("getUploadData",token);
	uni.request({
		url: data.url,
		method: data.method,
		header: data.header,
		data: {},
		success: response => {
			if(response.data.code === 0) {
				dry.set("uploadData", response.data);
			}
		}
	});
}

oss.upload = function(files, uploadData, token) {
	var fileList = [];
	var imageData = request.run("saveImage",token);
	files.forEach(e => {
		new Promise((resolve) => {
			var fileName = dry.makeRandOssObjectName() + dry.getFileExt(e.path);
			var accessKeyId = uploadData.data.credentials.accessKeyId;
			var uploadTask = uni.uploadFile({
				url: uploadData.domain,
				filePath: e.path, 
				name: "file",
				formData:{
					key: uploadData.dir + fileName,
					OSSAccessKeyId: accessKeyId,
					'x-oss-security-token': uploadData.data.credentials.securityToken,
					policy: uploadData.policy,
					signature: uploadData.signature,
					success_action_status: 200
				},
				success: res => {
					// console.log(res);
					if(res.statusCode == 200) {
						var file = {
							"url": uploadData.domain + "/" + uploadData.dir + fileName
						}
						uni.request({
							url: imageData.url,
							method: imageData.method,
							header: imageData.header,
							data: file,
							success: res => {
								if(res.statusCode === 200) {
									resolve(res.data);
								}
							}
						});
					}
				}
			});
			uploadTask.onProgressUpdate(res => {
				uni.showToast({
					title: "上传完成" + res.progress + "%"
				})
			})
		}).then(result => {
			fileList.push(result);
		})
	});
	return fileList;
}

oss.uploadFile = async function(fileList) {
	var token = dry.getToken();
	var uploadData = dry.get("uploadData");
	var expiration = new Date(uploadData.data.credentials.expiration);
	var now = new Date();
	if(expiration - now <= 0) {
		oss.getUploadData(token);
		uploadData = dry.get("uploadData");
	}
	var fileLists = await oss.upload(fileList, uploadData, token);
	console.log(fileLists);
	return fileLists;
}

oss.saveImg = function (file, token) {
	var imageData = request.run("saveImage",token);
	return new Promise((resolve) => {
		uni.request({
			url: imageData.url,
			method: imageData.method,
			header: imageData.header,
			data: file,
			success: res => {
				if(res.statusCode === 200) {
					resolve(res.data);
				}
			}
		});
	});
}

oss.getImagesByIds = function (ids) {
	var token = dry.getToken();
	var data = request.run("getImagesByIds", token);
	return new Promise((resolve) => {
		uni.request({
			url: data.url,
			method: data.method,
			header: data.header,
			data: ids,
			success: res => {
				resolve(res.data);
			}
		})
	})
}

oss.getImages = async function(ids) {
	return await oss.getImagesByIds(ids);
}

module.exports = oss;